#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>



int main()
{
  umask(0);//屏蔽命令行umask干扰
  if(mkfifo("./fifo", 0666) == -1)
  {
    perror("打开失败");
    return 1;
  }

  int fd = open("fifo", O_RDONLY);
  if(fd >= 0)
  {
    char buffer[64];
    while(1)
    {
      printf("服务端输出：");
      
      ssize_t ret = read(fd, buffer, sizeof(buffer) - 1);
      if(ret > 0)
      {
        buffer[ret] = '\0';
        printf("%s\n", buffer);
      }
      else if(ret == 0)
      {
        printf("客户端已经下线，服务端下线\n");
        break;
      }
      else 
      {
        perror("读取失败\n");
        break;
      }
    }
  }

  return 0;
}


